package com.vito.dcs.paver.construction.service.impl;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.BadSqlGrammarException;

import com.alibaba.dubbo.config.annotation.Service;
import com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException;
import com.vito.base.authoriza.service.SectionService;
import com.vito.base.factory.IDFactory;
import com.vito.base.model.DataGrid;
import com.vito.base.util.Validata;
import com.vito.dcs.common.util.SplitTableUtil;
import com.vito.dcs.paver.construction.dao.DataPaverConstructionMapper2;
import com.vito.dcs.paver.construction.model.DataPaverConstructionModel;
import com.vito.dcs.paver.construction.model.GpsUpData;
import com.vito.dcs.paver.construction.service.DataPaverConstructionService2;
import com.vito.dcs.report.service.impl.CalculateDistance;
import com.vito.dcs.sys.deviceSet.model.DeviceSetModel;
import com.vito.dcs.sys.deviceSet.service.DeviceSetService;

/**
 * DataPaverConstructionService接口实现类
 * @author  fangj E-mail: 
 * @version 0.1.1
 * @date    2019-4-19 10:33:02
 */
@Service
public class DataPaverConstructionServiceImpl2 implements DataPaverConstructionService2 {
	
	@Autowired
	private DataPaverConstructionMapper2 datapaverconstructionMapper;
	@Autowired
	private SectionService sectionService;
	@Autowired
	private DeviceSetService deviceSetService;
	
	private static final Logger log = LoggerFactory.getLogger(DataPaverConstructionServiceImpl2.class);
	
	private static final ConcurrentHashMap<String, Object> prevYLJDataMap = new ConcurrentHashMap<String, Object>();
	private static final ConcurrentHashMap<String, ConcurrentHashMap<String, Object>> prevTPJDataMap = new ConcurrentHashMap<String, ConcurrentHashMap<String, Object>>();
	
	private static volatile Date date = null;
	
	private static final String COMMON_TABLE_NAME = "tbl_dcs_data_paver_construction";
	private static final String COMMON_TABLE_SUFFIX = "_m";
	
	/**
	 * GPS数据上报
	 */
	@Override
	public void save(DataPaverConstructionModel dataPaverConstructionModel, GpsUpData gpsUpData){
		
		/**
		 * 处理GPS数据上报数据
		 */
		processGPSData_cancel500(dataPaverConstructionModel, gpsUpData);
		
		String tableName = SplitTableUtil.getTableName(dataPaverConstructionModel.getTime());
		dataPaverConstructionModel.setTableName(tableName);
		
		try {
			saveData(dataPaverConstructionModel, tableName);
		} catch (Exception e) {
			BadSqlGrammarException  b = (BadSqlGrammarException) e;
			Throwable cause = b.getCause();
			MySQLSyntaxErrorException m = null;
			try {
				 m = (MySQLSyntaxErrorException) cause;
			} catch (Exception e2) {
				throw e;
			}
			if(m != null && m.getErrorCode() == 1146){
				try {
					
					Map<String,Object> map = new HashMap<String, Object>();
					String tableName2 = SplitTableUtil.getTableName(dataPaverConstructionModel.getTime());
					map.put("table", tableName2);
					map.put("table_common", COMMON_TABLE_NAME);
					datapaverconstructionMapper.createTable(map);
					map.put("table", tableName2 + COMMON_TABLE_SUFFIX);
					datapaverconstructionMapper.createTable(map);
					
					saveData(dataPaverConstructionModel, tableName);
					
				} catch (Exception e2) {
					throw e2;
				}
				
			} else {
				throw e;
			}
		}
	}

	private void saveData(
			DataPaverConstructionModel dataPaverConstructionModel,
			String tableName) {
		if("0".equals(dataPaverConstructionModel.getIsRight())){
			datapaverconstructionMapper.saveOtherTable(dataPaverConstructionModel);
		}
		dataPaverConstructionModel.setTableName(tableName + COMMON_TABLE_SUFFIX);
		datapaverconstructionMapper.save(dataPaverConstructionModel);
	}
	
	/**
	 * 处理GPS数据上报数据
	 * @param dataPaverConstructionModel
	 * @param gpsUpData
	 */
	@SuppressWarnings("unused")
	private void processGPSData(
			DataPaverConstructionModel dataPaverConstructionModel,
			GpsUpData gpsUpData) {
		clearPrevMap(dataPaverConstructionModel, gpsUpData);
		
		if("2".equals(dataPaverConstructionModel.getData()) || "4".equals(dataPaverConstructionModel.getData())
				|| "5".equals(dataPaverConstructionModel.getData())){
			/*校正经纬度*/
			corLngAndLat(dataPaverConstructionModel, gpsUpData);
			/*计算当前里程*/
			String savePile = calCurrentMileage(dataPaverConstructionModel,gpsUpData);
			if(savePile != null && !"".equals(savePile)){
				/*根据上报时间查询工艺*/
				Map<String, Object> queryProcess = datapaverconstructionMapper.queryProcess(dataPaverConstructionModel);
				if(queryProcess != null && !"".equals(queryProcess.get("processType"))){
					/*判断是否是压路机及距离摊铺机是否500m*/
					if(!"TPJ".equals(dataPaverConstructionModel.getType())){
						
						ConcurrentHashMap<String, Object> concurrentHashMap = prevTPJDataMap.get(dataPaverConstructionModel.getSectionId());
						DataPaverConstructionModel TPJ_dpc = null;
						if(concurrentHashMap != null){
							Set<String> keySet = concurrentHashMap.keySet();
							for (String string : keySet) {
								TPJ_dpc = (DataPaverConstructionModel) concurrentHashMap.get(string);
								break;
							}
						}
						if(TPJ_dpc != null){
							/*存在摊铺机，计算压路机距摊铺机距离*/
							double lantitudeLongitudeDist = CalculateDistance.LantitudeLongitudeDist(dataPaverConstructionModel.getCorLng(), dataPaverConstructionModel.getCorLat(), TPJ_dpc.getCorLng(), TPJ_dpc.getCorLat());
							if(lantitudeLongitudeDist <= 500){
								calYLJSpeedAndAlarm(dataPaverConstructionModel,gpsUpData, queryProcess);
								calYLJTemperatureAndAlarm(dataPaverConstructionModel, gpsUpData,queryProcess);
								dataPaverConstructionModel.setCurrentMileage(savePile);
								dataPaverConstructionModel.setIsRight("0");
							} else {
								dataPaverConstructionModel.setIsRight("1");
							}
						} else {
							dataPaverConstructionModel.setIsRight("1");
						}
						
					} else {
						/*摊铺机*/
						calTPJSpeedAndArea(dataPaverConstructionModel,gpsUpData, queryProcess);
						processNearPile(dataPaverConstructionModel, gpsUpData,queryProcess);
						processTPJSegregationAndTempAlarm(dataPaverConstructionModel, gpsUpData,queryProcess);
						dataPaverConstructionModel.setCurrentMileage(savePile);
						dataPaverConstructionModel.setIsRight("0");
						
					}
				} else {
					/*工艺设置为空*/
					log.info("-------------> 工艺设置为null，丢弃数据");
				}
				
			} else {
				/*不存在当前里程*/
				log.info("-------------> 当前里程为null，丢弃数据");
				dataPaverConstructionModel.setIsRight("1");
			}
		} else {
			/*差分为一*/
			log.info("-------------> 差分等级为1，丢弃数据");
			dataPaverConstructionModel.setIsRight("1");
		}
	}
	/**
	 * 处理GPS数据上报数据
	 * @param dataPaverConstructionModel
	 * @param gpsUpData
	 */
	private void processGPSData_cancel500(
			DataPaverConstructionModel dataPaverConstructionModel,
			GpsUpData gpsUpData) {
		clearPrevMap(dataPaverConstructionModel, gpsUpData);
		
		if("2".equals(dataPaverConstructionModel.getData()) || "4".equals(dataPaverConstructionModel.getData())
				|| "5".equals(dataPaverConstructionModel.getData())){
			/*校正经纬度*/
			corLngAndLat(dataPaverConstructionModel, gpsUpData);
			/*计算当前里程*/
			String savePile = calCurrentMileage(dataPaverConstructionModel,gpsUpData);
			if(savePile != null && !"".equals(savePile)){
				
				dataPaverConstructionModel.setCurrentMileage(savePile);
				/*根据上报时间查询工艺*/
				Map<String, Object> queryProcess = datapaverconstructionMapper.queryProcess(dataPaverConstructionModel);
				if(queryProcess != null && !"".equals(queryProcess.get("processType"))){
					/*根据类型区分摊铺机和压路机，做不同处理*/
					if(!"TPJ".equals(dataPaverConstructionModel.getType())){
						/*压路机*/
						calYLJSpeedAndAlarm(dataPaverConstructionModel,gpsUpData, queryProcess);
						calYLJTemperatureAndAlarm(dataPaverConstructionModel, gpsUpData,queryProcess);
					} else {
						/*摊铺机*/
						calTPJSpeedAndArea_cancel500(dataPaverConstructionModel,gpsUpData, queryProcess);
						processNearPile(dataPaverConstructionModel, gpsUpData,queryProcess);
						processTPJSegregationAndTempAlarm(dataPaverConstructionModel, gpsUpData,queryProcess);
					}
					dataPaverConstructionModel.setIsRight("0");
					
				} else {
					/*工艺设置为空*/
					log.info("-------------> 工艺设置为null，丢弃数据");
					dataPaverConstructionModel.setIsRight("1");
				}
				
			} else {
				/*不存在当前里程*/
				log.info("-------------> 当前里程为null，丢弃数据");
				dataPaverConstructionModel.setIsRight("1");
			}
		} else {
			/*差分为一*/
			log.info("-------------> 差分等级为1，丢弃数据");
			dataPaverConstructionModel.setIsRight("1");
		}
	}
	
	/**
	 * 计算摊铺机速度和摊铺面积
	 * @param dataPaverConstructionModel
	 * @param gpsUpData
	 * @param queryProcess
	 */
	private void calTPJSpeedAndArea(
			DataPaverConstructionModel dataPaverConstructionModel,
			GpsUpData gpsUpData, Map<String, Object> queryProcess) {
		/*标段key ---> Map ---> key为con_id*/
		ConcurrentHashMap<String, Object> concurrentHashMap = prevTPJDataMap.get(dataPaverConstructionModel.getSectionId());
		if(concurrentHashMap != null){
			DataPaverConstructionModel dpc = (DataPaverConstructionModel) concurrentHashMap.get(dataPaverConstructionModel.getConId());
			
			if(dpc == null){
				
				ConcurrentHashMap<String, Object> chm = new ConcurrentHashMap<String, Object>();
				chm.put(dataPaverConstructionModel.getConId(), dataPaverConstructionModel);
				
				prevTPJDataMap.put(dataPaverConstructionModel.getSectionId(), chm);
				dataPaverConstructionModel.setRunSpeed(0.0);
				/**
				 * 计算摊铺面积
				 */
				calculateArea(gpsUpData,dataPaverConstructionModel,queryProcess, 0);
			} else {
				/**
				 * 更新
				 */
				concurrentHashMap.put(dataPaverConstructionModel.getConId(), dataPaverConstructionModel);
				long interval = (dataPaverConstructionModel.getTime().getTime() - dpc.getTime().getTime())/1000L;
				double distance = CalculateDistance.LantitudeLongitudeDist(dpc.getCorLng(), dpc.getCorLat(), dataPaverConstructionModel.getCorLng(), dataPaverConstructionModel.getCorLat());
				if(interval == 0L){
					dataPaverConstructionModel.setRunSpeed(0.0);
				} else {
					dataPaverConstructionModel.setRunSpeed(distance/interval);
				}
				/*速度报警*/
				processTPJSpeedAlarm(dataPaverConstructionModel, gpsUpData,queryProcess);
				/*计算摊铺面积*/		
				calculateArea(gpsUpData,dataPaverConstructionModel,queryProcess, distance);
			}
		} else {
			
			ConcurrentHashMap<String, Object> chm = new ConcurrentHashMap<String, Object>();
			chm.put(dataPaverConstructionModel.getConId(), dataPaverConstructionModel);
			
			prevTPJDataMap.put(dataPaverConstructionModel.getSectionId(), chm);
			dataPaverConstructionModel.setRunSpeed(0.0);
			
			/**
			 * 计算摊铺面积
			 */
			calculateArea(gpsUpData,dataPaverConstructionModel,queryProcess, 0);
		}
	}
	/**
	 * 计算摊铺机速度和摊铺面积
	 * @param dataPaverConstructionModel
	 * @param gpsUpData
	 * @param queryProcess
	 */
	private void calTPJSpeedAndArea_cancel500(
			DataPaverConstructionModel dataPaverConstructionModel,
			GpsUpData gpsUpData, Map<String, Object> queryProcess) {
		
		DataPaverConstructionModel dpc = (DataPaverConstructionModel) prevYLJDataMap.get(dataPaverConstructionModel.getConId());
		if(dpc == null){
			prevYLJDataMap.put(dataPaverConstructionModel.getConId(), dataPaverConstructionModel);
			dataPaverConstructionModel.setRunSpeed(0.0);
			/*计算摊铺面积*/
			calculateArea(gpsUpData,dataPaverConstructionModel,queryProcess, 0);
		} else {
			prevYLJDataMap.put(dataPaverConstructionModel.getConId(), dataPaverConstructionModel);
			
			long interval = (dataPaverConstructionModel.getTime().getTime() - dpc.getTime().getTime())/1000L;
			double distance = CalculateDistance.LantitudeLongitudeDist(dpc.getCorLng(), dpc.getCorLat(), dataPaverConstructionModel.getCorLng(), dataPaverConstructionModel.getCorLat());
			if(interval == 0L){
				dataPaverConstructionModel.setRunSpeed(0.0);
			} else {
				dataPaverConstructionModel.setRunSpeed(distance/interval);
			}
			/*速度报警*/
			processTPJSpeedAlarm(dataPaverConstructionModel, gpsUpData,queryProcess);
			/*计算摊铺面积*/		
			calculateArea(gpsUpData,dataPaverConstructionModel,queryProcess, distance);
		}
	}
	
	/**
	 * 处理摊铺机速度报警
	 * @param dataPaverConstructionModel
	 * @param gpsUpData
	 * @param queryProcess
	 */
	private void processTPJSpeedAlarm(
			DataPaverConstructionModel dataPaverConstructionModel,
			GpsUpData gpsUpData, Map<String, Object> queryProcess) {
		if(dataPaverConstructionModel.getRunSpeed()*60 > Double.parseDouble(queryProcess.get("maxSpeed").toString())){
			
			String alarm_msg = "速度超上限" + processPercent(dataPaverConstructionModel.getRunSpeed()*60, Double.parseDouble(queryProcess.get("maxSpeed").toString()));
			
			Map<String,Object> alarmMap = new HashMap<String, Object>();
			
			alarmMap.put("up_time", dataPaverConstructionModel.getTime());
			alarmMap.put("vehicle_no", dataPaverConstructionModel.getMac());
			alarmMap.put("vehicle_name", gpsUpData.getVehicleName());
			alarmMap.put("cur_mileage", dataPaverConstructionModel.getCurrentMileage());
			alarmMap.put("alarm_type", "超速");
			alarmMap.put("alarm_msg", alarm_msg);
			alarmMap.put("section_id", dataPaverConstructionModel.getSectionId());
			String alarm_id = "";
			if("01".equals(queryProcess.get("processType"))){
				alarm_id = IDFactory.getIDFactory().getIdInc("tbl_dcs_alarm_water")+"";
				
				alarmMap.put("alarm_id", alarm_id);
				datapaverconstructionMapper.saveAlarmWater(alarmMap);
				
			} else if("02".equals(queryProcess.get("processType"))){
				alarm_id = IDFactory.getIDFactory().getIdInc("tbl_dcs_alarm_asphalt")+"";
				
				alarmMap.put("alarm_id", alarm_id);
				datapaverconstructionMapper.saveAlarmAsphalt(alarmMap);
			}
		}
	}
	
	/**
	 * 处理摊铺机离析及温度报警
	 * @param dataPaverConstructionModel
	 * @param gpsUpData
	 * @param queryProcess
	 */
	private void processTPJSegregationAndTempAlarm(
			DataPaverConstructionModel dataPaverConstructionModel,
			GpsUpData gpsUpData, Map<String, Object> queryProcess) {
		if("02".equals(queryProcess.get("processType"))){
			
			Map<String, Object> querySegregation = datapaverconstructionMapper.querySegregation(dataPaverConstructionModel);
			if(querySegregation != null){
				
				String alarm_msg = "";
				String alarm_type = "";
				if(Double.parseDouble(dataPaverConstructionModel.getIos()) > Double.parseDouble(querySegregation.get("highSegregation").toString())){
					alarm_msg = "离析超上限" + processPercent(Double.parseDouble(dataPaverConstructionModel.getIos()),Double.parseDouble(querySegregation.get("highSegregation").toString()));
					alarm_type = "严重离析";
				} else if(Double.parseDouble(dataPaverConstructionModel.getIos()) > Double.parseDouble(querySegregation.get("lowerSegregation").toString())){
					alarm_msg = "离析超下限" + processPercent(Double.parseDouble(dataPaverConstructionModel.getIos()),Double.parseDouble(querySegregation.get("lowerSegregation").toString()));
					alarm_type = "轻度离析";
				}
				
				if(!"".equals(alarm_msg)){
					
					String alarm_id = IDFactory.getIDFactory().getIdInc("tbl_dcs_alarm_asphalt")+"";
					Map<String,Object> alarmMap = new HashMap<String, Object>();
					alarmMap.put("alarm_id", alarm_id);
					alarmMap.put("up_time", dataPaverConstructionModel.getTime());
					alarmMap.put("vehicle_no", dataPaverConstructionModel.getMac());
					alarmMap.put("vehicle_name", gpsUpData.getVehicleName());
					alarmMap.put("cur_mileage", dataPaverConstructionModel.getCurrentMileage());
					alarmMap.put("alarm_type", alarm_type);
					alarmMap.put("alarm_msg", alarm_msg);
					alarmMap.put("section_id", dataPaverConstructionModel.getSectionId());
					
					datapaverconstructionMapper.saveAlarmAsphalt(alarmMap);
					
				}
			}
			
			/**
			 * 温度报警
			 */
			/**
			 * 处理温度
			 */
			String processTemp = processTemp(gpsUpData);
			dataPaverConstructionModel.setTemperature(processTemp);
			
			/**
			 * 处理温度报警
			 */
			if(!Validata.isNullOrEmpty(processTemp) &&
				Double.parseDouble(processTemp) < Double.parseDouble(queryProcess.get("minTemperature").toString())){
				
				String alarm_id = IDFactory.getIDFactory().getIdInc("tbl_dcs_alarm_asphalt")+"";
				
				Map<String,Object> alarmMap = new HashMap<String, Object>();
				
				String alarm_msg = "温度超下限"+processPercent(Double.parseDouble(processTemp), Double.parseDouble(queryProcess.get("minTemperature").toString()));
				
				alarmMap.put("alarm_id", alarm_id);
				alarmMap.put("up_time", dataPaverConstructionModel.getTime());
				alarmMap.put("vehicle_no", dataPaverConstructionModel.getMac());
				alarmMap.put("vehicle_name", gpsUpData.getVehicleName());
				alarmMap.put("cur_mileage", dataPaverConstructionModel.getCurrentMileage());
				alarmMap.put("alarm_type", "温度超限");
				alarmMap.put("alarm_msg", alarm_msg);
				alarmMap.put("section_id", dataPaverConstructionModel.getSectionId());
				
				datapaverconstructionMapper.saveAlarmAsphalt(alarmMap);
				
			}
		}
	}
	
	/**
	 * 处理就近桩号
	 * @param dataPaverConstructionModel
	 * @param gpsUpData
	 * @param queryProcess
	 */
	private void processNearPile(
			DataPaverConstructionModel dataPaverConstructionModel,
			GpsUpData gpsUpData, Map<String, Object> queryProcess) {
		Map<String,Object> rm2 = new HashMap<String, Object>();
		rm2.put("sectionId", gpsUpData.getSectionId());
		rm2.put("longitude", dataPaverConstructionModel.getCorLng());
		rm2.put("latitude", dataPaverConstructionModel.getCorLat());

		String savePile2 = sectionService.savePile2(rm2);
		
		Map<String,Object> afterMap = new HashMap<String, Object>();
		
		afterMap.put("after_id", dataPaverConstructionModel.getId());
		afterMap.put("pile_key", savePile2);
		afterMap.put("time", dataPaverConstructionModel.getTime());
		afterMap.put("lng", dataPaverConstructionModel.getCorLng());
		afterMap.put("lat", dataPaverConstructionModel.getCorLat());
		afterMap.put("section_id", dataPaverConstructionModel.getSectionId());
		afterMap.put("layer_id", queryProcess.get("layerId"));
		afterMap.put("process_type", queryProcess.get("processType"));
		afterMap.put("con_id", dataPaverConstructionModel.getConId());
		
		/**
		 * 拆表后tbl_dcs_data_after_construction表作废
		 */
		//datapaverconstructionMapper.saveDataAfterCon(afterMap);
		datapaverconstructionMapper.updatePileKey(afterMap);
	}
	
	/**
	 * 计算压路机温度和报警
	 * @param dataPaverConstructionModel
	 * @param gpsUpData
	 * @param queryProcess
	 */
	private void calYLJTemperatureAndAlarm(
			DataPaverConstructionModel dataPaverConstructionModel,
			GpsUpData gpsUpData, Map<String, Object> queryProcess) {
		if("02".equals(queryProcess.get("processType"))){
			
			/**
			 * 处理温度
			 */
			String processTemp = processTemp(gpsUpData);
			dataPaverConstructionModel.setTemperature(processTemp);
			
			/**
			 * 处理温度报警
			 */
			if(!Validata.isNullOrEmpty(processTemp) &&
				Double.parseDouble(processTemp) < Double.parseDouble(queryProcess.get("minYLJTemperature").toString())){
				
				String alarm_id = IDFactory.getIDFactory().getIdInc("tbl_dcs_alarm_asphalt")+"";
				
				Map<String,Object> alarmMap = new HashMap<String, Object>();
				
				String alarm_msg = "温度超下限"+processPercent(Double.parseDouble(processTemp), Double.parseDouble(queryProcess.get("minYLJTemperature").toString()));
				
				alarmMap.put("alarm_id", alarm_id);
				alarmMap.put("up_time", dataPaverConstructionModel.getTime());
				alarmMap.put("vehicle_no", dataPaverConstructionModel.getMac());
				alarmMap.put("vehicle_name", gpsUpData.getVehicleName());
				alarmMap.put("cur_mileage", dataPaverConstructionModel.getCurrentMileage());
				alarmMap.put("alarm_type", "温度超限");
				alarmMap.put("alarm_msg", alarm_msg);
				alarmMap.put("section_id", dataPaverConstructionModel.getSectionId());
				
				datapaverconstructionMapper.saveAlarmAsphalt(alarmMap);
				
			}
		}
	}
	
	/**
	 * 计算压路机速度及速度报警
	 * @param dataPaverConstructionModel
	 * @param gpsUpData
	 * @param queryProcess
	 */
	private void calYLJSpeedAndAlarm(
			DataPaverConstructionModel dataPaverConstructionModel,
			GpsUpData gpsUpData, Map<String, Object> queryProcess) {
		DataPaverConstructionModel dpc = (DataPaverConstructionModel) prevYLJDataMap.get(dataPaverConstructionModel.getConId());
		if(dpc == null){
			prevYLJDataMap.put(dataPaverConstructionModel.getConId(), dataPaverConstructionModel);
			dataPaverConstructionModel.setRunSpeed(0.0);
		} else {
			/**
			 * 更新
			 */
			prevYLJDataMap.put(dataPaverConstructionModel.getConId(), dataPaverConstructionModel);

			long interval = (dataPaverConstructionModel.getTime().getTime() - dpc.getTime().getTime())/1000L;
			double distance = CalculateDistance.LantitudeLongitudeDist(dpc.getCorLng(), dpc.getCorLat(), dataPaverConstructionModel.getCorLng(), dataPaverConstructionModel.getCorLat());
			if(interval == 0L){
				dataPaverConstructionModel.setRunSpeed(0.0);
			} else {
				dataPaverConstructionModel.setRunSpeed(distance/interval);
			}
			/**
			 * 速度报警
			 */
			
			Map<String, Object> queryRollerAlarm = datapaverconstructionMapper.queryRollerAlarm(dataPaverConstructionModel);
			
			if(queryRollerAlarm != null){
				
				String alarm_msg = "";
				if(dataPaverConstructionModel.getRunSpeed()*3.2 > Double.parseDouble(queryRollerAlarm.get("levelThreeLimit").toString())){
					alarm_msg = "超三级报警"+processPercent(dataPaverConstructionModel.getRunSpeed()*3.2, Double.parseDouble(queryRollerAlarm.get("levelThreeLimit").toString()));
				} else if(dataPaverConstructionModel.getRunSpeed()*3.2 > Double.parseDouble(queryRollerAlarm.get("levelTwoLimit").toString())){
					alarm_msg = "超二级报警"+processPercent(dataPaverConstructionModel.getRunSpeed()*3.2, Double.parseDouble(queryRollerAlarm.get("levelTwoLimit").toString()));
				} else if(dataPaverConstructionModel.getRunSpeed()*3.2 > Double.parseDouble((String)queryRollerAlarm.get("levelOneLimit"))){
					alarm_msg = "超一级报警"+processPercent(dataPaverConstructionModel.getRunSpeed()*3.2, Double.parseDouble(queryRollerAlarm.get("levelOneLimit").toString()));
				}
				if(!"".equals(alarm_msg)){
					Map<String,Object> alarmMap = new HashMap<String, Object>();
					
					alarmMap.put("up_time", dataPaverConstructionModel.getTime());
					alarmMap.put("vehicle_no", dataPaverConstructionModel.getMac());
					alarmMap.put("vehicle_name", gpsUpData.getVehicleName());
					alarmMap.put("cur_mileage", dataPaverConstructionModel.getCurrentMileage());
					alarmMap.put("alarm_type", "超速");
					alarmMap.put("alarm_msg", alarm_msg);
					alarmMap.put("section_id", dataPaverConstructionModel.getSectionId());
					
					String alarm_id = "";
					if("01".equals(queryProcess.get("processType"))){
						alarm_id = IDFactory.getIDFactory().getIdInc("tbl_dcs_alarm_water")+"";
						
						alarmMap.put("alarm_id", alarm_id);
						datapaverconstructionMapper.saveAlarmWater(alarmMap);
						
					} else if("02".equals(queryProcess.get("processType"))){
						alarm_id = IDFactory.getIDFactory().getIdInc("tbl_dcs_alarm_asphalt")+"";
						
						alarmMap.put("alarm_id", alarm_id);
						datapaverconstructionMapper.saveAlarmAsphalt(alarmMap);
					}
				}
			}
		}
	}

	private String calCurrentMileage(
			DataPaverConstructionModel dataPaverConstructionModel,
			GpsUpData gpsUpData) {
		Map<String,Object> rm = new HashMap<String, Object>();
		rm.put("sectionId", gpsUpData.getSectionId());
		rm.put("longitude", dataPaverConstructionModel.getCorLng());
		rm.put("latitude", dataPaverConstructionModel.getCorLat());

		String savePile = sectionService.savePile(rm);
		return savePile;
	}

	private void corLngAndLat(
			DataPaverConstructionModel dataPaverConstructionModel,
			GpsUpData gpsUpData) {
		DeviceSetModel deviceSetModel = new DeviceSetModel();
		deviceSetModel.setSectionId(gpsUpData.getSectionId());
		DeviceSetModel queryDeviceSetBySectionId = deviceSetService.queryDeviceSetBySectionId(deviceSetModel);
		if(queryDeviceSetBySectionId != null){
			dataPaverConstructionModel.setCorLng(gpsUpData.getLng()+Double.parseDouble(queryDeviceSetBySectionId.getLngCorrection()==null?0+"":queryDeviceSetBySectionId.getLngCorrection()));
			dataPaverConstructionModel.setCorLat(gpsUpData.getLat()+Double.parseDouble(queryDeviceSetBySectionId.getLatCorrection()==null?0+"":queryDeviceSetBySectionId.getLatCorrection()));
		} else {
			dataPaverConstructionModel.setCorLng(gpsUpData.getLng());
			dataPaverConstructionModel.setCorLat(gpsUpData.getLat());
		}
	}

	private void clearPrevMap(
			DataPaverConstructionModel dataPaverConstructionModel,
			GpsUpData gpsUpData) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String format = sdf.format(gpsUpData.getTime());
		
		if(date != null){
			
			String format2 = sdf.format(date);
			
			if(!format.equals(format2)){
				
				prevTPJDataMap.clear();
				prevYLJDataMap.clear();
				
			}
		}
		
		/*更新date*/
		date = dataPaverConstructionModel.getTime();
	}
	
	/**
	 * 计算温度平均值
	 * @param gpsUpData
	 * @return
	 */
	private static String processTemp(GpsUpData gpsUpData){
		int count = 0;
		double value = 0.0;
		if(!"error".equals(gpsUpData.getVal_1()) && !"null".equals(gpsUpData.getVal_1())){
			value = value + Double.parseDouble(gpsUpData.getVal_1());
			count ++;
		} 
		if(!"error".equals(gpsUpData.getVal_2()) && !"null".equals(gpsUpData.getVal_2())){
			value = value + Double.parseDouble(gpsUpData.getVal_2());
			count ++;
		}
		if(!"error".equals(gpsUpData.getVal_3()) && !"null".equals(gpsUpData.getVal_3())){
			value = value + Double.parseDouble(gpsUpData.getVal_3());
			count ++;
		} 
		if(!"error".equals(gpsUpData.getVal_4()) && !"null".equals(gpsUpData.getVal_4())){
			value = value + Double.parseDouble(gpsUpData.getVal_4());
			count ++;
		}
		if(!"error".equals(gpsUpData.getVal_5()) && !"null".equals(gpsUpData.getVal_5())){
			value = value + Double.parseDouble(gpsUpData.getVal_5());
			count ++;
		}

		if(count > 0){
			value = Double.parseDouble(new DecimalFormat("###.######").format(value/count));
		}
		return value+"";
	}
	private void calculateArea(com.vito.dcs.paver.construction.model.GpsUpData gpsUpData,
			DataPaverConstructionModel dataPaverConstructionModel,
			Map<String, Object> queryProcess, double distance) {
		Map<String,Object> areaMap = new HashMap<String, Object>();
		areaMap.put("con_id", dataPaverConstructionModel.getConId());
		areaMap.put("section_id", dataPaverConstructionModel.getSectionId());
		areaMap.put("up_time", dataPaverConstructionModel.getUpTimeInd());
		
		if("01".equals(queryProcess.get("processType"))){
			
			String id = IDFactory.getIDFactory().getIdInc("tbl_dcs_water_area")+"";
			areaMap.put("id", id);
			
			Integer queryWaterAreaCount = datapaverconstructionMapper.queryWaterAreaCount(areaMap);
			Map<String, Object> queryWaterTotalWeight = datapaverconstructionMapper.queryWaterTotalWeight(dataPaverConstructionModel);
			if(queryWaterTotalWeight == null){
				queryWaterTotalWeight = new HashMap<String, Object>();
				queryWaterTotalWeight.put("totalWeight", 0);
			}
			if(queryWaterAreaCount > 0){
				
				double area = Double.parseDouble(String.format("%.2f", distance*(Double.parseDouble(gpsUpData.getVehicleWidth()))));
				areaMap.put("area", area);
				datapaverconstructionMapper.updateWaterArea(areaMap);
				
				/*更新轨迹*/
				Map<String,Object> traceMap = new HashMap<String, Object>();
				traceMap.put("sectionId", dataPaverConstructionModel.getSectionId());
				traceMap.put("trans_date", dataPaverConstructionModel.getUpTimeInd());
				traceMap.put("end_mileage", dataPaverConstructionModel.getCurrentMileage());
				Map<String, Object> queryWaterArea = datapaverconstructionMapper.queryWaterArea(areaMap);
				traceMap.put("paving_area", queryWaterArea.get("area") == null ? 0 : queryWaterArea.get("area"));
				traceMap.put("total_discharge", queryWaterTotalWeight.get("totalWeight"));
				
				datapaverconstructionMapper.updateTraceWater(traceMap);
				
			} else {
				datapaverconstructionMapper.saveWaterArea(areaMap);
				
				/*处理轨迹*/
				String traceId = IDFactory.getIDFactory().getIdInc("tbl_dcs_trace_water")+"";
				Map<String, Object> traceMap = processTraceMap(dataPaverConstructionModel, queryProcess,queryWaterTotalWeight, traceId);
				datapaverconstructionMapper.saveTraceWater(traceMap);
			}
			
		} else if("02".equals(queryProcess.get("processType"))){
			String id = IDFactory.getIDFactory().getIdInc("tbl_dcs_asphalt_area")+"";
			areaMap.put("id", id);
			
			Integer queryAsphaltAreaCount = datapaverconstructionMapper.queryAsphaltAreaCount(areaMap);
			Map<String, Object> queryAsphaltTotalWeight = datapaverconstructionMapper.queryAsphaltTotalWeight(dataPaverConstructionModel);
			if(queryAsphaltTotalWeight == null){
				queryAsphaltTotalWeight = new HashMap<String, Object>();
				queryAsphaltTotalWeight.put("totalWeight", 0);
			}
			if(queryAsphaltAreaCount > 0){
				double area = Double.parseDouble(String.format("%.2f", distance*(Double.parseDouble(gpsUpData.getVehicleWidth()))));
				areaMap.put("area", area);
				datapaverconstructionMapper.updateAsphaltArea(areaMap);
				
				/*更新轨迹*/
				Map<String,Object> traceMap = new HashMap<String, Object>();
				traceMap.put("sectionId", dataPaverConstructionModel.getSectionId());
				traceMap.put("trans_date", dataPaverConstructionModel.getUpTimeInd());
				traceMap.put("end_mileage", dataPaverConstructionModel.getCurrentMileage());
				Map<String, Object> queryAsphaltArea = datapaverconstructionMapper.queryAsphaltArea(areaMap); 
				traceMap.put("paving_area", queryAsphaltArea.get("area") == null ? 0 : queryAsphaltArea.get("area"));
				traceMap.put("total_discharge", queryAsphaltTotalWeight.get("totalWeight"));
				
				datapaverconstructionMapper.updateAsphaltArea(traceMap);
				
			} else {
				datapaverconstructionMapper.saveAsphaltArea(areaMap);
				
				/*处理轨迹*/
				String traceId = IDFactory.getIDFactory().getIdInc("tbl_dcs_trace_asphalt")+"";
				Map<String, Object> traceMap = processTraceMap(dataPaverConstructionModel, queryProcess,queryAsphaltTotalWeight, traceId);
				datapaverconstructionMapper.saveTraceAsphalt(traceMap);
				
			}
			
		}
	}

	private Map<String, Object> processTraceMap(
			DataPaverConstructionModel dataPaverConstructionModel,
			Map<String, Object> queryProcess,
			Map<String, Object> queryWaterTotalWeight, String traceId) {
		Map<String,Object> traceMap = new HashMap<String, Object>();
		traceMap.put("id", traceId);
		traceMap.put("sectionId", dataPaverConstructionModel.getSectionId());
		traceMap.put("trans_date", dataPaverConstructionModel.getUpTimeInd());
		traceMap.put("start_mileage", dataPaverConstructionModel.getCurrentMileage());
		traceMap.put("end_mileage", dataPaverConstructionModel.getCurrentMileage());
		traceMap.put("layer_id", queryProcess.get("layerId"));
		traceMap.put("paving_area", 0);
		traceMap.put("total_discharge", queryWaterTotalWeight.get("totalWeight") == null ? 0 : queryWaterTotalWeight.get("totalWeight"));
		return traceMap;
	}
	
	private static String processPercent(double d1,double d2){
		if(d2 == 0){
			return 0+"%";
		} else {
			return new DecimalFormat("#.##").format(Math.abs(d1-d2)/d2*100) + "%";
		}
	}
	
	@Override
	public DataGrid<DataPaverConstructionModel> queryAllByPage(DataPaverConstructionModel datapaverconstruction){
		DataGrid<DataPaverConstructionModel> dg = new DataGrid<DataPaverConstructionModel>();
		dg.setRows(datapaverconstructionMapper.queryAllByPage(datapaverconstruction));
		dg.setTotal(datapaverconstruction.getTotal());
		return dg;
	}
	@Override
	public DataPaverConstructionModel query(DataPaverConstructionModel datapaverconstruction){
		return datapaverconstructionMapper.query(datapaverconstruction);
	}
	@Override
	public Integer update(DataPaverConstructionModel datapaverconstruction){
		return datapaverconstructionMapper.update(datapaverconstruction);
	}
	
	@Override
	public Integer delete(DataPaverConstructionModel datapaverconstruction){
		return datapaverconstructionMapper.delete(datapaverconstruction);
	}

    @Override
	public int saveDataPaverConstruction(DataPaverConstructionModel datapaverconstruction){
	    datapaverconstruction.setId(IDFactory.getIDFactory().getIdInc()+"");                               
		//return save(datapaverconstruction);
	    return 0;
	}
	@Override
	public Map<String, Object> queryProcess(
			DataPaverConstructionModel datapaverconstruction) {
		return datapaverconstructionMapper.queryProcess(datapaverconstruction);
	}
	@Override
	public Map<String, Object> queryRollerAlarm(
			DataPaverConstructionModel datapaverconstruction) {
		return datapaverconstructionMapper.queryRollerAlarm(datapaverconstruction);
	}
	@Override
	public void saveAlarmWater(Map<String, Object> map) {
		datapaverconstructionMapper.saveAlarmWater(map);
	}
	@Override
	public void saveAlarmAsphalt(Map<String, Object> map) {
		datapaverconstructionMapper.saveAlarmAsphalt(map);
	}
	@Override
	public void saveDataAfterCon(Map<String, Object> map) {
		datapaverconstructionMapper.saveDataAfterCon(map);
	}
	@Override
	public Map<String, Object> querySegregation(
			DataPaverConstructionModel datapaverconstruction) {
		return datapaverconstructionMapper.querySegregation(datapaverconstruction);
	}
	@Override
	public void saveWaterArea(Map<String, Object> map) {
		datapaverconstructionMapper.saveWaterArea(map);
	}
	@Override
	public void saveAsphaltArea(Map<String, Object> map) {
		datapaverconstructionMapper.saveAsphaltArea(map);
	}
	@Override
	public void updateWaterArea(Map<String, Object> map) {
		datapaverconstructionMapper.updateWaterArea(map);
	}
	@Override
	public void updateAsphaltArea(Map<String, Object> map) {
		datapaverconstructionMapper.updateAsphaltArea(map);
	}
	@Override
	public Integer queryWaterAreaCount(Map<String, Object> map) {
		return datapaverconstructionMapper.queryWaterAreaCount(map);
	}
	@Override
	public Integer queryAsphaltAreaCount(Map<String, Object> map) {
		return datapaverconstructionMapper.queryAsphaltAreaCount(map);
	}
}